import { createStore, combineReducers, applyMiddleware } from "redux";
import thunk from "redux-thunk"
//引入一个创建saga的方法
import createSagaMiddleware from "redux-saga";
//定义的saga
import { myRoom } from "./saga/index";
//创建一个saga 中间件
const sagaMiddleware = createSagaMiddleware();

//写一个函数用来获取所有的reucers
function getReducers() {
    let files = require.context("./reducers", true, /\.js$/)

    return files.keys().reduce((pre, item) => {
        let key = item.match(/\.\/(\w+)\.js$/)[1];
        pre[key] = files(item).default
        return pre
    }, {})
}

const reducers = combineReducers(getReducers());

const store = createStore(reducers, applyMiddleware(thunk, sagaMiddleware));
//运行saga
sagaMiddleware.run(myRoom);
export default store;